package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {

        //保存菜品的基本信息
        this.save(setmealDto);
        Long setmealId=setmealDto.getId();

        //套餐内菜品
        List<SetmealDish> setmealDishes=setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味数据表
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        //如果套餐处在启售状态，则不能删除
        int count = this.count(queryWrapper);
        if(count>0){
            throw new CustomException("套餐正在售卖，不能删除");
        }

        this.removeByIds(ids);

        //删除套餐内菜品，对应setmeal_dish表
        LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper1);

    }

    /**
     * 根据id查询套餐和套餐内的菜品
      * @param id
     * @return
     */
    public SetmealDto getByIdWithDish(Long id){
        //获取套餐对象
        Setmeal setmeal=this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        //将setmeal中的属性传给setmealDto中，setmealDto中还缺少套餐内菜品信息
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询套餐内的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        //将菜品信息集合list
        List<SetmealDish> list=setmealDishService.list(queryWrapper);
        //为setmealDto设置属性菜品信息集合list
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }


    /**
     * 更新套餐同时更新套餐内菜品
     * @param setmealDto
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新dish表
        this.updateById(setmealDto);

        //清理当前套餐内菜品，对应setmeal_dish的delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //添加当前套餐内菜品，对应setmeal_dish的insert操作
        List<SetmealDish> dishes=setmealDto.getSetmealDishes();
        dishes=dishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }


}
